import requests
import json
from src.utils.wechat_push_utils import WechatPushTool
import time
import akshare as ak

class RealTimeStockScanner:
    def __init__(self, wechat_push_tool):
        self.wechat_push_tool = wechat_push_tool
        self.rules = []

    def add_rule(self, rule):
        self.rules.append(rule)

    def scan_stock(self, stock_data):
        for rule in self.rules:
            if rule(stock_data):
                message = f'股价数据触发规则：{stock_data}'
                self.wechat_push_tool.push_message('user', message)

    def scan_convertible_bonds(self):
        # 获取市场上所有可转债数据
        bond_df = ak.bond_cov_comparison()
        for index, row in bond_df.iterrows():
            bond_data = {
                'name': row['债券名称'],
                'price': row['转债价格']
            }
            # 假设这里有一个获取支撑位的函数 get_support_level
            support_level = self.get_support_level(bond_data['name'])
            if abs(bond_data['price'] - support_level) < 10:  # 价格接近支撑位，可根据实际情况调整差值
                message = f'可转债 {bond_data['name']} 价格接近支撑位，当前价格: {bond_data['price']}'
                self.wechat_push_tool.push_message('user', message)

    def get_support_level(self, bond_name):
        # 这里可以实现获取支撑位的逻辑，例如从数据库或文件中读取
        return 100  # 示例支撑位

if __name__ == '__main__':
    # 使用示例，需要替换为实际的企业微信信息
    push_tool = WechatPushTool()
    scanner = RealTimeStockScanner(push_tool)
    
    # 示例规则：股价大于100
    def example_rule(data):
        return data['price'] > 100
    
    scanner.add_rule(example_rule)
    
    # 定时扫描功能
    while True:
        # 示例股价数据
        example_stock_data = {'price': 101}
        scanner.scan_stock(example_stock_data)
        # 定时扫描可转债功能
        while True:
            scanner.scan_convertible_bonds()
            time.sleep(60) # 每分钟扫描一次